package expressToAltaRica;

//Cette class nous montre comment lire un fichier XML avec DOM

import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

/*import javax.xml.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;*/
import org.xml.sax.SAXException;

public class Test {

	public static  void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException{
		//lire un document xml
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.parse("mytest.xml");
		
		//Obtenir le premier element du xml et faire des operations dessus
		Element racine = doc.getDocumentElement();
		
		
		//Parcourir la liste des activators et recupere linterface correspondante
		//et la rajoute dans la balise activator

		 NodeList activatorList = doc.getElementsByTagName("Mfha_activator");
		 NodeList ioList = doc.getElementsByTagName("Mfha_out");
		 for(int i = 0;i<activatorList.getLength();i++){
			 Element activator = (Element) activatorList.item(i);
			 Element act = (Element) activator.getFirstChild();
			 String act_id = act.getAttributeNode("ref").getValue();
			 
			 for(int j=0;j<ioList.getLength();j++){
				 Element io = (Element) ioList.item(j);
				 String io_id = io.getAttributeNode("id").getValue();
				 if(act_id.equalsIgnoreCase(io_id)){
					 act.getParentNode().replaceChild(io,act);
				 } 
			 }
			 
		 }

		  
		TransformerFactory tfact = TransformerFactory.newInstance();
		Transformer transformer = tfact.newTransformer();
		transformer.setOutputProperty("encoding", "ISO-8859-1");
		DOMSource source = new DOMSource(doc);
		FileWriter fw = new FileWriter("interm.xml");
		StreamResult result = new StreamResult(fw);
		transformer.transform(source, result);
		
	       }
		 
	}
	


